﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Commons;
using DAL;
using SimpComm;
using ViewModel;

namespace Bll
{
    public class BaseService<T> where T : class, new()
    {
        /// <summary>
        /// EF实例化(注意:基类只负责增删改查)
        /// </summary>
        private BaseEFDal<T> dal = new BaseEFDal<T>();

        public BaseEFDal<T> Dal
        {
            get { return dal; }
            set { dal = value; }
        }


        public virtual int GetCount(Func<T, bool> exp)
        {
            return dal.GetEntitiesCount(exp);
        }

        public virtual bool Any(Func<T, bool> exp)
        {
            return dal.Any(exp);
        }

        #region 获取单条

        public virtual DataInfo<T> GetEntity(Func<T, bool> exp)
        {
            var data = dal.GetEntity(exp);
            if (data == null)
                return new DataInfo<T>() { Success = false, Message = "查询无数据" };
            return new DataInfo<T>() { Data = data };
        }

        public virtual DataInfo<T> GetAEntity(Func<T, bool> exp, DynamicOrderby dynamicOrderby = null)
        {
            var data = dal.GetFirstEntity(exp, dynamicOrderby);
            if (data == null)
                return new DataInfo<T>() { Success = false, Message = "查询无数据" };
            return new DataInfo<T>() { Data = data };
        }

        public DataInfo<TModel> GetModel<TModel>(Func<T, bool> func, Func<T, TModel> select)
        {
            var data = dal.GetModel(func, select);
            if (data == null)
                return new DataInfo<TModel>() { Success = false, Message = "查询无数据" };
            return new DataInfo<TModel>() { Data = data };
        }

        public DataInfo<TModel> GetAModel<TModel>(Func<T, bool> exp, Func<T, TModel> select, DynamicOrderby dynamicOrderby = null)
        {
            var data = dal.GetFirstModel(exp, select, dynamicOrderby);
            if (data == null)
                return new DataInfo<TModel>() { Success = false, Message = "查询无数据" };
            return new DataInfo<TModel>() { Data = data };
        }
        #endregion

        #region 获取多条

        public virtual IEnumerable<T> GetEntities(Func<T, bool> exp)
        {
            return dal.GetEntities(exp);
        }

        public TModel GetModel<TModel>(Func<T, bool> func, Func<T, TModel> select, DynamicOrderby dyOrderby)
        {
            return dal.GetFirstModel<TModel>(func, select, dyOrderby);
        }

        public MD<TModel> SM<TModel>(SMP<T, TModel> s)
        {
            return dal.SM<TModel>(s);
        }
        #endregion

        #region 增删改查
        public virtual MsgBox Insert(T data, Action<T> defaultValueFun = null)
        {
            return dal.Insert(data, defaultValueFun);
        }

        public virtual MsgBox Update(Expression<Func<T, bool>> predicate, Expression<Func<T, T>> updater)
        {
            return dal.Update(predicate, updater);
        }

        public virtual MsgBox Delete(T data)
        {
            return dal.Delete(data);
        }

        public virtual MsgBox Delete(Object ID)
        {
            return dal.Delete(ID);
        }

        public virtual MsgBox Delete(Expression<Func<T, bool>> filter)
        {
            return dal.Delete(filter);
        }

        public virtual MsgBox UpdateOrInsertByID(Expression<Func<T, T>> updater, Expression<Func<T, bool>> predicate, Action<T> defaultValueFun = null, string ID = "ID")
        {
            Type t = typeof(T);
            var d = predicate.Compile();
            var ndata = new T();
            t.GetProperty(ID).SetValue(ndata, 0, null);
            bool isnew = d(ndata);
            if (isnew)
            {
                T data = updater.Body as T;
                return Insert(data, null);
            }
            else
            {
                return dal.Update(predicate, updater);
            }
        }
        #endregion

    }
}
